/*
 * 文 件 名 : ViceQuestionActivity.java
 * 版    权 : China Machi.Co.,Ltd.Copyright 2009-2011.All rights reserved
 * 描    述 : <描述>
 * 修 改 人 : lixg
 * 修改时间 : 2013-1-3
 * 跟踪单号 : <跟踪单号>
 * 修改单号 : <修改单号>
 * 修改内容 : <修改内容>
 */
package com.fire8.dolphin.ui;

import java.io.File;
import java.io.IOException;
import java.util.Date;

import com.fire8.dolphin.db.QuestionDAO;
import com.fire8.dolphin.model.Question;
import com.fire8.dolphin.service.SubmitQuestion;
import com.fire8.dolphin.utils.DateUtil;
import com.fire8.dolphin.utils.LogUtil;

import android.app.Activity;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.view.View.OnTouchListener;
import android.widget.ImageButton;
import android.widget.TextView;

/**
 * 语音问题
 * 在我的问题列表页面点击语音按钮，弹出录音页面
 *
 * @author  lixg
 * @version [版本号,2013-1-3]
 * @see     [相关类/方法]
 * @since   [产品/模块]
 */
public class VoiceQuestionActivity extends Activity
{

    private LayoutInflater inflater;
    private TextView tvDesc;
    private MediaRecorder mRecorder = null;
    private File mRecAudioPath;
    private File mRecAudioFile;
    
    DolphinApplication app;
    
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.input_type_voic);
        app = (DolphinApplication)getApplication();
        this.setTitle("请按住说话");
        tvDesc = (TextView)findViewById(R.id.tv_desc);
        tvDesc.setTextColor(R.color.white);
        tvDesc.setText("00:00");
        
        ImageButton btnRecord = (ImageButton)findViewById(R.id.btn_input_voice_record);
        btnRecord.setOnLongClickListener(new OnLongClickListener()
        {
            
            @Override
            public boolean onLongClick(View v)
            {
                startRecording();
                return false;
            }
        });
        
        btnRecord.setOnTouchListener(new OnTouchListener()
        {
            @Override
            public boolean onTouch(View v, MotionEvent event)
            {
                if(MotionEvent.ACTION_UP == event.getAction())
                {
                    //停止录音
                    stopRecording();
                    
                    if(mRecAudioFile != null)
                    {
                        LogUtil.d("完成录音： "+mRecAudioFile.getPath()+" "+ mRecAudioFile.getName());
                        LogUtil.d("文件大小(字节)："+mRecAudioFile.length());
                        //保存问题到本地
                        QuestionDAO dao = new QuestionDAO(VoiceQuestionActivity.this);
                        Question question  = new Question();
                        question.setTitle("");
                        question.setContext(mRecAudioFile.getName());
                        question.setUpdateDate(DateUtil.dateToStr(new Date(), "yyyy-MM-dd hh:mm:ss"));
                        question.setAnswerNum("0");
                        question.setUserId(app.getUserId());
                        //question.setDataType(Question.DATA_TYPE_TXT);
                        dao.add(question);
                        
                        //调用service提交问题到服务器并将返回的问题ID更新到本地
                        SubmitQuestion submit = new SubmitQuestion(VoiceQuestionActivity.this,app);
                        submit.execute(question);
                        VoiceQuestionActivity.this.finish();
                    }
                    
                }
                return false;
            }
        });
    }
    

    private void startRecording() {
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                mRecAudioPath = Environment.getExternalStorageDirectory();
                File fPath = new File(mRecAudioPath.getPath() + File.separator
                                + "RecordList");
                fPath.mkdirs();
                LogUtil.d("创建文件夹"+fPath.getPath());
                mRecAudioFile = fPath;
        } else {
            mRecAudioFile = this.getCacheDir();    
            LogUtil.d("没有插卡或没有读写卡权限，创建文件夹");
        }
        try {
                mRecAudioFile = File.createTempFile(String.valueOf(System.currentTimeMillis()), ".3gp", mRecAudioFile);
                LogUtil.d("创建文件： "+mRecAudioFile.getPath()+" "+ mRecAudioFile.getName());
        } catch (IOException e) {
            LogUtil.e("mRec", e);
        }
        
        mRecorder = new MediaRecorder();
        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        mRecorder.setOutputFile(mRecAudioFile.getPath());
        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        try {
            mRecorder.prepare();
            mRecorder.start();
            LogUtil.d("开始录音...");
        } catch (IOException e) {
            LogUtil.e( "prepare() failed",e);
        }
        
        
    }
    private void stopRecording() {
        if(mRecorder == null)
            return;
        //if(mRecorder.)
        mRecorder.stop();
        mRecorder.release();
        mRecorder = null;
        LogUtil.d("停止录音...");
    }
}
